package com.sky.controller.admin;

import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.SetmealService;
import com.sky.vo.SetmealVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author 冰白寒祭
 * @date 2023-09-02 09:00:04
 * @description 套餐管理
 */
@RestController
@RequestMapping("/admin/setmeal")
@Api(tags = "套餐相关接口")
@Slf4j
public class SetmealController {

    @Autowired
    private SetmealService setmealService;

    /**
     * @MethodName: save
     * @Author 冰白寒祭
     * @Description: 新增套餐
     * @Date: 2023-09-02 09:02:04
     * @param: SetmealDTO setmealDTO
     * @return: Result
     */
    @PostMapping
    @ApiOperation("新增套餐")
    @CacheEvict(cacheNames = "setmealCache", key = "#setmealDTO.categoryId")//清理缓存
    public Result save(@RequestBody SetmealDTO setmealDTO) {
        setmealService.saveWithDish(setmealDTO);
        return Result.success();
    }

    /**
     * @MethodName: page
     * @Author 冰白寒祭
     * @Description: 套餐分页查询
     * @Date: 2023-09-02 09:30:52
     * @param: SetmealPageQueryDTO setmealPageQueryDTO
     * @return: Result<PageResult>
     */
    @GetMapping("/page")
    @ApiOperation("套餐分页查询")
    public Result<PageResult> page(SetmealPageQueryDTO setmealPageQueryDTO) {
        PageResult pageResult = setmealService.pageQuery(setmealPageQueryDTO);
        return Result.success(pageResult);
    }

    /**
     * @MethodName: delete
     * @Author 冰白寒祭
     * @Description: 批量删除套餐
     * @Date: 2023-09-02 09:56:50
     * @param: List<long> ids
     * @return: Result
     */
    @DeleteMapping
    @ApiOperation("批量删除套餐")
    @CacheEvict(cacheNames = "setmealCache", allEntries = true)//清理缓存
    public Result delete(@RequestParam List<Long> ids) {
        setmealService.deleteBatch(ids);
        return Result.success();
    }

    /**
     * @MethodName: getById
     * @Author 冰白寒祭
     * @Description: 根据id查询套餐，用于修改页面回显数据
     * @Date: 2023-09-02 10:15:19
     * @param: Long id
     * @return: SetmealVO
     */

    @GetMapping("/{id}")
    @ApiOperation("根据id查询套餐")
    public Result<SetmealVO> getById(@PathVariable Long id) {
        SetmealVO setmealVO = setmealService.getByIdWithDish(id);
        return Result.success(setmealVO);
    }

    /**
     * @MethodName: update
     * @Author 冰白寒祭
     * @Description: 修改套餐
     * @Date: 2023-09-02 10:19:32
     * @param: SetmealDTO setmealDTO
     * @return: Result
     */
    @PutMapping
    @ApiOperation("修改套餐")
    @CacheEvict(cacheNames = "setmealCache", allEntries = true)//清理缓存
    public Result update(@RequestBody SetmealDTO setmealDTO) {
        setmealService.update(setmealDTO);
        return Result.success();
    }


    /**
     * @MethodName: startOrStop
     * @Author 冰白寒祭
     * @Description: 套餐起售停售
     * @Date: 2023-09-02 11:57:32
     * @param: Integer status
     * @param: Long id
     * @reurn: Result
     */

    @PostMapping("/status/{status}")
    @ApiOperation("套餐起售停售")
    @CacheEvict(cacheNames = "setmealCache", allEntries = true)//清理缓存
    public Result startOrStop(@PathVariable Integer status, Long id) {
        setmealService.startOrStop(status, id);
        return Result.success();
    }
}
